# PowerShell Startup Script - Disable QuickEdit Mode
# Encoding: UTF-8

$host.UI.RawUI.WindowTitle = "Backend Service - Running"

# Disable QuickEdit Mode
try {
    $signature = @'
[DllImport("kernel32.dll")]
public static extern IntPtr GetStdHandle(int nStdHandle);

[DllImport("kernel32.dll")]
public static extern bool GetConsoleMode(IntPtr hConsoleHandle, out uint lpMode);

[DllImport("kernel32.dll")]
public static extern bool SetConsoleMode(IntPtr hConsoleHandle, uint dwMode);
'@

    $type = Add-Type -MemberDefinition $signature -Name 'Win32' -Namespace 'ConsoleAPI' -PassThru
    
    $STD_INPUT_HANDLE = -10
    $ENABLE_QUICK_EDIT = 0x0040
    
    $handle = $type::GetStdHandle($STD_INPUT_HANDLE)
    $mode = 0
    $type::GetConsoleMode($handle, [ref]$mode) | Out-Null
    $mode = $mode -band (-bnot $ENABLE_QUICK_EDIT)
    $type::SetConsoleMode($handle, $mode) | Out-Null
} catch {
    Write-Host "Warning: Cannot disable QuickEdit mode" -ForegroundColor Yellow
}

Write-Host ""
Write-Host "========================================"
Write-Host "  Backend Service Startup (PowerShell)"
Write-Host "========================================"
Write-Host ""
Write-Host "QuickEdit mode disabled - Safe to view logs"
Write-Host ""
Write-Host "Current Directory: $PWD"
Write-Host ""

# Check Python
try {
    $pythonVersion = python --version 2>&1
    Write-Host "[OK] $pythonVersion"
} catch {
    Write-Host "Error: Python not found" -ForegroundColor Red
    Write-Host "Tip: Please install Python 3.7+" -ForegroundColor Yellow
    Read-Host "Press any key to exit"
    exit 1
}

Write-Host ""

# Check main.py
if (-not (Test-Path "main.py")) {
    Write-Host "Error: main.py not found" -ForegroundColor Red
    Read-Host "Press any key to exit"
    exit 1
}

Write-Host "[OK] Project files checked"
Write-Host ""

# Start Service
Write-Host "========================================"
Write-Host "Starting Service (Port: 2233)"
Write-Host "========================================"
Write-Host ""
Write-Host "URL: http://localhost:2233" -ForegroundColor Green
Write-Host "API Docs: http://localhost:2233/docs" -ForegroundColor Green
Write-Host ""
Write-Host "Features:"
Write-Host "   - WeChat Content Security Check (Text + Image)"
Write-Host "   - Local Sensitive Words Detection"
Write-Host "   - User Management + Real-name Auth"
Write-Host "   - Activity Management + Friends System"
Write-Host "   - Payment (Alipay + WeChat Pay)"
Write-Host ""
Write-Host "QuickEdit disabled - Clicking window will not pause service" -ForegroundColor Cyan
Write-Host "Press Ctrl+C to stop service" -ForegroundColor Yellow
Write-Host ""

# Run Python service
python main.py

Read-Host "`nPress any key to exit"
